<?php
declare (strict_types=1);

namespace app\api\controller;

use addons\WeChatMini\facade\WeChatMini;
use app\common\model\Assets;
use app\common\model\Relation;
use app\common\model\User as UserModel;
use app\common\model\UserNotify;
use app\common\tool\Result;
use think\Response;

class User extends Base
{
    public function index(): Response
    {
        $account = Assets::getFields(3);
        $assets = [];
        foreach ($account as $key => $val) {
            $assets[] = [
                'title' => $val,
                'type' => $key,
                'balance' => $this->user->assets->$key
            ];
        }
        $result = [
            'id' => $this->user->id,
            'code' => $this->user->code,
            'nickname' => $this->user->nickname,
            'mobile' => $this->user->mobile,
            'photo' => $this->user->photo,
            'assets' => $assets,
            'level_name' => $this->user->level ? $this->user->level->title : '',
            'notify_num' => UserNotify::where(['uid' => $this->user->id, 'status' => 0])->count(),
            'order_1' => getOrderCount($this->user->id, 1),
            'order_2' => getOrderCount($this->user->id, 2),
            'order_3' => getOrderCount($this->user->id, 3),
            'is_method' => '1',
            'is_remit' => $this->system['remit_open'],
            'is_draw' => $this->system['draw_open'],
            'is_transfer' => $this->system['transfer_open'],
            'is_exchange' => $this->system['exchange_open'],
            'is_share' => $this->user->is_active == 2 ? '1' : '0'
        ];
//        if (!$this->system['examine_open']) {
//            $result['is_method'] = '0';
//            $result['is_remit'] = '0';
//            $result['is_draw'] = '0';
//            $result['is_transfer'] = '0';
//            $result['is_exchange'] = '0';
//        }

        return Result::success('success', $result);
    }

    public function info(): Response
    {
        $parent = UserModel::where(['id' => $this->user->parent_id])->find();
        $result = [
            'id' => $this->user->id,
            'code' => $this->user->code,
            'nickname' => $this->user->nickname,
            'mobile' => $this->user->mobile,
            'photo' => $this->user->photo,
            'parent_code' => $parent ? $parent->code : '',
            'level_name' => $this->user->level ? $this->user->level->title : ''
        ];

        return Result::success('success', $result);
    }

    public function edit(): Response
    {
        $param = $this->request->post();

        return Result::fastReturn(UserModel::_edit($param, $this->user));
    }

    public function myTeamTotal(): Response
    {
        $result = [
            'team_total' => $this->user->team_total,
            'team_count' => Relation::where(['uid' => $this->user->id])->count()
        ];

        return Result::success('success', $result);
    }

    public function myTeamList(): Response
    {
        $sort = [
            ['key' => 'is_active', 'value' => 'desc'],
            ['key' => 'active_time', 'value' => 'asc'],
            ['key' => 'id', 'value' => 'asc']
        ];
        $list = UserModel::getPageList(['parent_id' => $this->user->id],
            'id,code,nickname,mobile,photo,my_total,team_total,level_id',
            $this->getPage(), $this->getLimit(), false, ['level'], [], $sort);
        foreach ($list['data'] as &$v) {
            $v->level_name = $v->level ? $v->level->title : '';
            unset($v->level);
        }

        return Result::fastReturn($list);
    }

    public function getInvitationCode(): Response
    {
        try {
            $result = WeChatMini::getMiniScene($this->user->code, $this->user->code, 'pages/index/index', '/qrcode/', 'trial');

        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }

        return Result::success('success', $result);
    }
}